﻿using HamcoWcs.Entitys.BaseConfigs;
using HamcoWcs.Models.Common.Enums;
using HamcoWcs.Models.DeviceMsgs;
using HamcoWcs.Models.Devices;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;

namespace HamcoWcs.Models.Monitors
{
    public class MonitorConData
    {

        public string Name { get; set; }
        public string PortState { get; set; }
        public string PconName { get; set; }
        public int PconMode { get; set; }
        public int ErrorCode { get; set; }
        public int IsGoods { get; set; }
        public int DestLoc { get; set; }
        public int CheckRes { get; set; }
        public int CheckResult { get; set; }
        public string UnitId { get; set; }
        // ppk 2023/03/27需要显示方向  1：入库模式|2：出库模式|3：入出模式
        public string WorkType { get; set; }
        public string Request { get; set; }
        public string Des { get; set; }
        public ushort Speed { get; set; }
        public UInt32 DevLoc { get; set; }
        public uint Input { get; set; }
        public uint Output { get; set; }
        public string PalletType { get; set; }
        public string DisableDesc { get; set; }
        public UInt32 RgvPosition { get; set; }

        public List<uint> Inputs { get; set; } = new List<uint>();
        public List<uint> Outputs { get; set; } = new List<uint>();


        public MonitorConData()
        {

        }

        public MonitorConData(PconDto pconDto)
        {
            Name = pconDto.DeviceName;
            Update(pconDto);
        }

        public void Update(PconDto pconDto)
        {
            PconName = $"P{pconDto.PconName.ToString().PadLeft(4, '0')}";
            PconMode = pconDto.PconMode;
            ErrorCode = pconDto.ErrorCode;
            IsGoods = pconDto.IsGoods;
            if (pconDto.IsGoods == 0 && pconDto.PconMode == 6)
            {
                IsGoods = 1;
            }

            CheckRes = pconDto.CheckRes;
            CheckResult = pconDto.CheckResult;
            //Weight = pconDto.Weight;
            Speed = pconDto.Speed;
            UnitId = pconDto.UnitId;
            WorkType = GetWorkType(pconDto.WorkType);
            Request = ((EnumPconRecvMsg)pconDto.Request).ToString();
            //Size = pconDto.Size;
            DevLoc = pconDto.DevLoc;
            Input = pconDto.Input;
            if (Inputs.Count >= 30) Inputs.RemoveAt(0);
            Inputs.Add(Input);
            Output = pconDto.Output;
            if (Outputs.Count >= 30) Outputs.RemoveAt(0);
            Outputs.Add(Output);
            PortState = pconDto.PortState;

            DestLoc = pconDto.DestLoc;
            Des = GetDes(pconDto.PconMode);
            PalletType = pconDto.PalletType;
            DisableDesc = pconDto.DisableDesc;
            RgvPosition = pconDto.RgvPosition;
        }

        private string GetWorkType(int workType)
        {
            switch (workType)
            {
                case 1: return "入库模式";
                case 2: return "出库模式";
                case 3: return "双向模式";
                case 4: return "人工投料";
                default: return "";
            }
        }

        private string GetDes(int mode)
        {
            switch (mode)
            {
                case 0: return "设备离线";
                case 1: return "设备运行";
                case 2: return "设备手动";
                case 3: return "设备停止";
                case 4: return GetAlarm(this.ErrorCode.ToString());
                case 5: return "设备维护";
                case 6: return "动作中";
                case 7: return "占位中";
                case 8: return "禁用";
                default: return "";
            }

        }

        private string GetAlarm(string code)
        {
            var a = Alarms.FirstOrDefault(x => x.Module == "pcon" && x.Code == code);
            if (a is null) return "未知异常";
            return a.AlarmName;
        }

        public static List<AlarmExplanation> Alarms = new List<AlarmExplanation>();

        public static void RefreshAlarms(List<AlarmExplanation> alarms)
        {
            Alarms.Clear();
            Alarms.AddRange(alarms);
        }
    }

}
